<template>
  <div class="xtx-city">
    <div class="select" @click="toggleDialog" :class="{ active }">
      <span class="placeholder" v-show="!changeResult.provinceName"
        >请选择配送地址</span
      >
      <span class="value" v-show="changeResult">
        {{ changeResult.provinceName }}-{{ changeResult.cityName }}-{{
          changeResult.countyName
        }}</span
      >
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-if="active">
      <span class="ellipsis" @click="city(i)" v-for="i in cityList" :key="i">{{
        i.name
      }}</span>
    </div>
  </div>
</template>
<script>
import { onMounted, ref, reactive, watch } from 'vue'
import axios from 'axios'
export default {
  name: 'XtxCity',
  emits: ['chang'],
  setup (props, ctx) {
    // 控制展开收起,默认收起
    const active = ref(false)
    // 存储请求来的信息
    const cityList = ref([])
    // 存储一份请求来信息
    const cityData = ref([])
    // 切换展开收起
    function toggleDialog () {
      active.value = !active.value
      if (active.value === false) {
        // 传值
        ctx.emit('chang', changeResult)
      }
    }
    // h获取城市数据
    function geicityList () {
      axios({
        url: 'https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json',
        method: 'get'
      }).then(res => {
        console.log('ml', res)
        cityList.value = res.data
        // 深拷贝
        cityData.value = JSON.parse(JSON.stringify(res.data))
      })
    }
    // 声明响应数据 存数据
    const changeResult = reactive({
      provinceCode: '', // 省code
      provinceName: '', // 省名称
      cityCode: '', // 城市code
      cityName: '', // 城市名称
      countyCode: '', // 地区code
      countyName: '' // 地区名称
    })
    function city (i) {
      cityList.value = i.areaList
      if (i.level === 0) {
        changeResult.provinceCode = i.code
        changeResult.provinceName = i.name
      }
      if (i.level === 1) {
        changeResult.cityCode = i.code
        changeResult.cityName = i.name
      }
      if (i.level === 2) {
        changeResult.countyCode = i.code
        changeResult.countyName = i.name
        toggleDialog()
      }
    }
    onMounted(() => {
      geicityList()
    })
    // 监听 每次打开 需要重新填写
    watch(
      () => {
        // 监听div是否显示
        return active.value
      },
      // val得到当前active.value的值
      (val) => {
        // 如果为true 给被渲染的list重新赋值 这样数据就会重新渲染
        if (val === true) {
          cityList.value = cityData.value
        }
      }
    )

    return { active, toggleDialog, cityList, city, changeResult, cityData }
  }
}
</script>
<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  margin-left: 10px;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>
